package com.cz.background.service.common;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import com.cz.background.common.dto.PageResponseResult;
import com.cz.background.common.exception.BizException;
import com.cz.background.dto.common.role.AddRoleDTO;
import com.cz.background.dto.common.role.RoleQueryDTO;
import com.cz.background.dto.common.role.UpdateRoleDTO;
import com.cz.background.dto.common.role.UpdateRoleStatusDTO;
import com.cz.background.dto.common.user.UpdateUserDTO;
import com.cz.background.entity.common.SysRole;
import com.cz.background.mapper.common.SysRoleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper ;

    public PageResponseResult<SysRole> queryRolePage(RoleQueryDTO roleQueryDTO){
        PageHelper.startPage(roleQueryDTO.getPageNum(), roleQueryDTO.getPageSize());
        List<SysRole> sysRoles = sysRoleMapper.findByAll(roleQueryDTO);
        PageInfo<SysRole> pageInfo = new PageInfo<>(sysRoles);
        PageResponseResult<SysRole> pageResponseResult = new PageResponseResult<>();
        pageResponseResult.setRecords(sysRoles);
        pageResponseResult.setPage(pageInfo.getPages());
        pageResponseResult.setTotal(pageInfo.getTotal());
        pageResponseResult.setPageSize(pageInfo.getPageSize());
        pageResponseResult.setPageNumber(pageInfo.getPageNum());
        return pageResponseResult;
    }

    public void addRole(@Valid AddRoleDTO addRoleDTO) {
        if(addRoleDTO != null){
            SysRole sysRole =  BeanUtil.copyProperties(addRoleDTO, SysRole.class);
            sysRole.setId(IdUtil.getSnowflakeNextIdStr());
            sysRoleMapper.insert(sysRole);
        }
    }

    public void delRole(List<String> ids) {
        if(CollUtil.isEmpty(ids)){
            throw new BizException("500","参数不能为空");
        }
         sysRoleMapper.deleteByIdIn(ids);
    }



    public void updateUser(UpdateUserDTO updateUserDTO) {
       String id = updateUserDTO.getId();
       SysRole sysRole = sysRoleMapper.selectByPrimaryKey(id);
       if(sysRole == null) {
           throw new BizException("500", "用户不存在");
       }
       BeanUtil.copyProperties(updateUserDTO, sysRole);
       sysRoleMapper.updateByPrimaryKey(sysRole);
    }

    public void updateRole(@Valid UpdateRoleDTO updateRoleDTO) {
        String id = updateRoleDTO.getId();
        SysRole sysRole = sysRoleMapper.selectByPrimaryKey(id);
        if(sysRole == null) {
            throw new BizException("500", "角色不存在");
        }
        BeanUtil.copyProperties(updateRoleDTO, sysRole);
        sysRoleMapper.updateByPrimaryKey(sysRole);
    }

    public void updateRoleStatus(@Valid UpdateRoleStatusDTO updateRoleStatusDTO) {
        sysRoleMapper.updateRoleStatus(updateRoleStatusDTO);
    }

    public List<SysRole> queryRoleList() {
        return sysRoleMapper.findByAll(null);
    }
}
